package com.iuie.ns.system.tags;

import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iuie.ns.system.service.code.ISysCodeInfoService;
import com.iuie.ns.system.tags.beans.SysDropCodeTypeVO;
import com.iuie.ns.system.vo.code.SysCodeInfoVO;
import com.iuie.ns.web.html.HTMLTag;

public class DropCodeSetTag extends SimpleTagSupport {

	private String id;
    
    private String codeTypes;
    
    private Boolean full = false;
    
    private ISysCodeInfoService sysCodeService;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public String getCodeTypes() {
		return codeTypes;
	}
	public void setCodeTypes(String codeTypes) {
		this.codeTypes = codeTypes;
	}
	public Boolean getFull() {
		return full;
	}
	public void setFull(Boolean full) {
		this.full = full;
	}

	@Override
	public void doTag() throws JspException, IOException {
		JspWriter writter = getJspContext().getOut();
		ServletContext servletContext = ((PageContext) this.getJspContext()).getServletContext();
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		sysCodeService = (ISysCodeInfoService) wac.getBean("codeInfoService");
		
		Set<String> typeIds = new HashSet<>();
		String[] typeIdArr = codeTypes.split(",");
		if (typeIdArr != null && typeIdArr.length > 0) {
			for (int i = 0; i < typeIdArr.length; i++) {
				String type = typeIdArr[i];
				typeIds.add(type);
			}
		}
		
		List<SysDropCodeTypeVO> list = sysCodeService.findCodeInfosByTypeIdArray(typeIds);
		
		// 完整的js
		StringBuilder jsBuilder = new StringBuilder();
		/*
		{
			"typeid":{
			    "value1":"",
			    "value2":""
		    }
		}
		 */
        String singleDrop = this.getSingleDropCode(list);
        jsBuilder.append(singleDrop);
        
        // full默认为false
        if (this.full) {
        	String fullDrop = this.getFullDropCode(list);
        	jsBuilder.append(fullDrop);
        }
        
        HTMLTag tag = new HTMLTag("script");
        tag.addAttribute("type", "text/javascript");
        tag.setTextNode(jsBuilder.toString());
        
        writter.write(tag.toString());
	}
	
	// 获取全量的codeSet
	private String getFullDropCode(List<SysDropCodeTypeVO> list) throws JsonProcessingException {
		Iterator<SysDropCodeTypeVO> it = list.iterator();
		Map<String, Object> droplist = new LinkedHashMap<String, Object>();
		
		while(it.hasNext()) {
			SysDropCodeTypeVO codeType = it.next();
			droplist.put(codeType.getCodeTypeId(), codeType.getCodeList());
		}
		
		ObjectMapper mapper = new ObjectMapper();
        
        String jsObj = mapper.writeValueAsString(droplist);
        String jsFull = "var full_" + id + " = " + jsObj + ";";
		return jsFull;
	}

	// 获取简单的codeSet
	private String getSingleDropCode(List<SysDropCodeTypeVO> list) throws JsonProcessingException {
		
		Iterator<SysDropCodeTypeVO> it = list.iterator();
		Map<String, Object> droplist = new LinkedHashMap<String, Object>();
		while(it.hasNext()) {
			SysDropCodeTypeVO codeType = it.next();
			Map<String,	Object> codeInfos = new LinkedHashMap<>();
			if (codeType.getCodeList() != null && codeType.getCodeList().size() > 0) {
				for (SysCodeInfoVO info:codeType.getCodeList()) {
					codeInfos.put(info.getCodeInfoValue(), info.getCodeInfoName());
				}
			}
			droplist.put(codeType.getCodeTypeId(), codeInfos);
		}
		ObjectMapper mapper = new ObjectMapper();
        
        String jsObj = mapper.writeValueAsString(droplist);
        String jsSingle = "var " + id + " = " + jsObj + ";";
        return jsSingle;
	}
	
}
